/* eslint-disable no-restricted-syntax */
/* eslint-disable no-empty */
/* eslint-disable no-param-reassign */
import { reactive } from 'vue'

export interface IDoor {
  height: 1350 | 1850 | 2050
  width: 800 | 900 | 1000 | 1200 | 1400
  color: 'BLK' | 'YLW' | 'RAL7035' | 'RAL5017' | 'RAL3000' | 'RAL9016'
  spacing: number[]
  silk: string
  type: 'JLM' | 'PYM' // JLM: 铰链门  PYM:平移门
  direction: 'LEFT' | 'RIGHT'
  lock: 'A' | 'B'
}

export const Door: IDoor = {
  height: 1850,
  width: 1000,
  color: 'BLK',
  spacing: [25, 100],
  silk: '3.0',
  type: 'JLM',
  direction: 'LEFT',
  lock: 'B'
}

export function createDoor() {
  return reactive(Door)
}

export class DoorClass implements IDoor {
  points: number[]

  angle: number

  height: 1350 | 1850 | 2050

  width: 800 | 900 | 1000 | 1200 | 1400

  color: 'BLK' | 'YLW' | 'RAL7035' | 'RAL5017' | 'RAL3000' | 'RAL9016'

  spacing: number[]

  silk: string = '3.0'

  type: 'JLM' | 'PYM'

  direction: 'LEFT' | 'RIGHT'

  lock: 'A' | 'B'

  constructor(
    points: number[] = [0, 0],
    angle: number = 0,
    height: 1350 | 1850 | 2050 = 1850,
    width: 800 | 900 | 1000 | 1200 | 1400 = 1000,
    color: 'BLK' | 'YLW' | 'RAL7035' | 'RAL5017' | 'RAL3000' | 'RAL9016' = 'BLK',
    spacing: number[] = [25, 100],
    silk: string = '3.0',
    type: 'JLM' | 'PYM' = 'JLM',
    direction: 'LEFT' | 'RIGHT' = 'LEFT',
    lock: 'A' | 'B' = 'B'
  ) {
    this.points = points
    this.angle = angle
    this.height = height
    this.width = width
    this.color = color
    this.spacing = spacing
    this.silk = silk
    this.type = type
    this.direction = direction
    this.lock = lock
  }

  getName() {
    return `KKDR-${this.type}-${this.height}x${this.width}-${this.spacing[0]}x${this.spacing[1]}x${this.silk}-${this.color}-SET`
  }

  getLockName() {
    return `KKCK-LCK-${this.lock}-SET`
  }

  getDoorInfo() {
    return {
      model: this.getName(),
      size: `${this.height}x${this.width}`,
      type: this.type,
      color: this.color,
      spacing: `${this.spacing[0]}x${this.spacing[1]}x${this.silk}`,
      direction: this.direction,
      lock: this.lock
    }
  }

  getLockInfo() {
    return {
      model: this.getLockName(),
      lock: this.lock
    }
  }

  static fromString(name: string) {
    const str = name.split('-')
    if (str.length !== 6) return null
    return {
      model: name,
      size: str[2],
      type: str[1],
      color: str[4],
      spacing: str[3]
    }
  }

  getGadget(pillar: number[] = [40, 60]) {
    const dataList: any[] = []

    dataList.push({
      type: `${this.type}-${this.direction}`,
      count: 1
    })

    dataList.push({
      model: `KKGD-${this.height}x${this.width}-${this.spacing[0]}x${this.spacing[1]}x${this.silk}-${this.color}`,
      size: `${this.height}x${this.width}`,
      type: '网板',
      color: this.color,
      count: this.type === 'JLM' ? 1 : 2
    })
    dataList.push({
      type: '门档',
      model: `KKDR-BEM-20x50x${this.width + (pillar[0] === 40 ? 100 : 200)}-YLW`,
      size: `20x50x${this.width + (pillar[0] === 40 ? 100 : 200)}`,
      color: 'YLW',
      count: 1
    })
    dataList.push({ type: '门档固定件', model: `KKDR-FXS-YLW-SET`, color: 'YLW', count: 2 })
    dataList.push({ type: '门拉手', model: `KKDR-HD-SET`, count: 1 })
    dataList.push({ type: '门滑轮固定件套件', model: `KKDR-PLY-BLK-SET`, color: 'BLK', count: 1 })
    dataList.push({ type: '锁', model: `KKCK-LCK-${this.lock}-SET`, count: 1 })
    if (this.type === 'JLM') {
      dataList.push({ type: '铰链', model: `KKDR-HG-40x60-BLK-SET`, color: 'BLK', count: 3 })
      dataList.push({ type: '门停', model: `KKDR-STP-BLK-SET`, color: 'BLK', count: 1 })
    } else if (this.type === 'PYM') {
      dataList.push({
        type: '滑轨套件',
        model: `KKGD-SDW-76x${this.width / 2}-EZ-SET`,
        color: 'BLK',
        count: 1
      })
      dataList.push({ type: '滑轨固定件组件', model: 'KKDR-ZJSDW-NI-SET', count: 6 })
    }
    return dataList
  }
}

export function createDoorAction(door: IDoor) {
  return {
    updateHeigh: (height: 1350 | 1850 | 2050) => {
      door.height = height
    },
    updateWidth: (width: 800 | 900 | 1000 | 1200 | 1400) => {
      door.width = width
    },
    updateColor: (color: 'BLK' | 'YLW' | 'RAL7035' | 'RAL5017' | 'RAL3000' | 'RAL9016') => {
      door.color = color
    },
    updateSpacing: (spacing: [25, 100]) => {
      door.spacing = spacing
    },
    updateSilk: (silk: string = '3.0') => {
      door.silk = silk
    },
    updateType: (type: 'JLM' | 'PYM') => {
      door.type = type
    },
    updateDirection: (direction: 'LEFT' | 'RIGHT') => {
      door.direction = direction
    },
    updateLock: (lock: 'A' | 'B') => {
      door.lock = lock
    }
  }
}
